package com.ruoyi.flow.core.response;

/**
 * 通用响应对象, 封装数据如下
 * <pre>
 *    {"meta":{"code":200,"message":"创建成功"},"data":{"id":"5308e9c2-a4ce-4dca-9373-cc1ffe63d5f9","name":"Apple Watch SPORT","description":"Sport 系列的表壳材料为轻巧的银色及深空灰色阳极氧化铝金属，强化 Ion-X 玻璃材质为显示屏提供保护。搭配高性能 Fluoroelastomer 表带，共有 5 款缤纷色彩。"}}
 * </pre>
 *
 * <b>使用样例：</b>
 * <p>生成成功响应：</p>
 * <pre>
 *     Response<String> resp = Response.<String>success().setData("hello world!");
 * </pre>
 *
 * <p>生成失败响应：</p>
 * <pre>
 *     Response<String> resp = Response.<String>error();
 * </pre>
 *
 * <p>生成自定义响应：</p>
 * <pre>
 *     Response<String> resp = Response.meta().setCode(403).setMessage("无权限访问").<String>create();
 * </pre>
 *
 * @author wtian
 */
public class Response<T> {
    private Meta meta;

    private T data;

    Response(Meta meta) {
        this.meta = meta;
    }

    public Meta getMeta() {
        return meta;
    }

    public T getData() {
        return data;
    }

    public Response<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 创建响应元信息
     *
     * @return
     */
    public static Meta meta() {
        return new Meta();
    }

    /**
     * 生成成功的响应信息
     * @param <T>
     * @return
     */
    public static <T> Response<T> success(){
        return Response.meta().success().<T>create();
    }

    /**
     * 生成错误响应信息
     * @param <T>
     * @return
     */
    public static <T> Response<T> error(){
        return Response.meta().error().<T>create();
    }


    public static void main(String[] args) {
        Response<String> response = Response.<String>success().setData("hello");
        System.out.println(response);
    }
}
